home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / openwindow / openwindow.p < prev    next >
Encoding:
Text File  |  2000-06-23  |  5.0 KB  |  137 lines

  1. {
  2.     File:        OpenWindow.p
  3.  
  4.     Contains:    
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. }
  23. PROGRAM OpenWindow;
  24.  
  25. USES    Windows,OSUtils,ToolUtils,SegLoad,Fonts,Retrace,
  26.         QuickDraw,Resources,Events,Memory,Script;
  27.  
  28. VAR
  29.     WindTitle : Str255;
  30.     MyWindow,aWindPtr : WindowPtr;
  31.     MyEvent : EventRecord;
  32.     quit,DrawOn : boolean;
  33.     WindRect : Rect;
  34.     aPoint : point;
  35.     myCrsr : CursHandle;
  36.     
  37. {------------------------------------------------------------------------------------}
  38.  
  39. PROCEDURE _DataInit;    EXTERNAL;
  40.  
  41. {------------------------------------------------------------------------------------}
  42.  
  43. PROCEDURE InitMac;
  44.  
  45. BEGIN                       {InitMac}    {the big inits}
  46.  
  47.     {UnLoadSeg(@_DataInit);               remove data initialization code before any allocations}
  48.     InitGraf(@qd.thePort);                {initialize QuickDraw}
  49.     InitFonts;                           {initialize Font Manager}
  50.     FlushEvents(everyEvent, 0);        {call OS Event Mgr to discard any previous events}
  51.     InitWindows;                       {initialize Window Manager}
  52.     InitCursor;                        {call QuickDraw to make cursor (pointer) an arrow}
  53.     quit := false;
  54.     DrawOn := false;
  55.  
  56. END;                       {InitMac}
  57.  
  58. {------------------------------------------------------------------------------------}
  59.  
  60. PROCEDURE DoDraw;
  61.  
  62. BEGIN
  63.     DrawOn := true;
  64.     GlobalToLocal (Myevent.where);
  65.     MoveTo (Myevent.where.h,Myevent.where.v);
  66.     EraseRect (WindRect);
  67. END;
  68.  
  69. {------------------------------------------------------------------------------------}
  70.  
  71. BEGIN                              {main PROGRAM}
  72.  
  73.     InitMac;                                {'nitialize them Mac Managers}
  74.  
  75.     {Now let's figure out how big the (main) screen is, and make a Rect that fits inside it}
  76.     {Screenbits.bounds always has the Rect of the main screen}
  77.     
  78.     WindRect.top := qd.screenBits.bounds.top + GetMBarHeight+25;
  79.     WindRect.left := qd.screenBits.bounds.left + 5;
  80.     WindRect.bottom := qd.screenBits.bounds.bottom - 5;
  81.     WindRect.right := qd.screenBits.bounds.right - 5;
  82.     
  83.     WindTitle := ('Press "q" to quit');        {the window title will tell the user how to quit this program}
  84.     
  85.     MyWindow := NewWindow                    {we'll make a window with NewWindow, cuz we don't want to carry}
  86.                                             { a resource around}
  87.                            (nil,            {let the window manager take care of storing the window record}
  88.                            WindRect,        {this is our "custom tailored to the scereen" rect}
  89.                            WindTitle,        {our famous "user interface in the window title" title}
  90.                            true,            {the visible boolean, we'd like to see our} 
  91.                            noGrowDocProc,    {this tells NewWindow what kind of window we want}
  92.                            Pointer(-1),        {this param is the pointer to the window we want to be behind}
  93.                                                { in this case, we want to be frontmost, and Pointer(-1) does that}
  94.                            true,            {this is true, so we can have a "go away" box in the title bar}
  95.                            1);                {refCon - a reference number; used by the app for whatever it wants to.}
  96.  
  97.     if MyWindow <> nil then                    {make sure the new window pointer is valid}
  98.     begin
  99.         SetPort (MyWindow);                    {set the current graf port to our new window}
  100.         GlobalToLocal (WindRect.topLeft);    {make our Window Rect local coords so erase rect will do the right thing}
  101.         GlobalToLocal (WindRect.botRight);
  102.         myCrsr := GetCursor (crossCursor);    {we'll use the crosshair cursor}
  103.         if myCrsr <> nil then
  104.         begin
  105.             MoveHHi (handle(myCrsr));        {move the cursor data high in the heap to avoid fragmentation}
  106.             HLock (handle(myCrsr));            {now lock the handle down}
  107.             SetCursor (cursor(myCrsr^^));    {and finally set the cursor to the crosshair}
  108.             repeat
  109.                 if not DrawOn then            {DrawOn is sort of a "mouse down in content region" flag}
  110.                 begin
  111.                     if GetNextEvent (EveryEvent,MyEvent) then    {only check for events when mouse is up}
  112.                     
  113.                         CASE MyEvent.what of
  114.                         
  115.                             mouseDown : if ((FindWindow (MyEvent.where,aWindPtr) = inContent)    {make sure the mouseDown is}
  116.                                                                                                 {where we want it}
  117.                                         and (aWindptr = MyWindow)) then DoDraw
  118.                                         else if FindWindow (MyEvent.where,aWindPtr) = inGoAway then Quit := true;
  119.                         
  120.                             KeyDown : if (char(BitAnd (MyEvent.message,charCodeMask)) = 'q') then quit := true;
  121.                             
  122.                         end; {case}
  123.                 end
  124.                 else
  125.                 begin
  126.                     If StillDown then        {if mouse btn is still down, keep drawing}
  127.                     begin
  128.                         GetMouse (aPoint);    {see where the mouse is pointing now}
  129.                         StdLine (aPoint);    { and draw a line to it}
  130.                     end
  131.                     else DrawOn := false;    {if the mouse btn isn't down, stop drawing}
  132.                 end;
  133.             until quit;
  134.         end;
  135.     end;
  136.         
  137. END.